package com.jxwifi.cloud.quickclean.complaint;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.idroid.utils.NoDoubleClickListener;
import com.idroid.widget.Toaster;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.SysDicBean;
import com.jxwifi.cloud.quickclean.bean.UploadBean;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;
import com.jxwifi.cloud.quickclean.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.orderdetails.OrderDetailsActivity;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.photo.PhotoPickerActivity;
import com.umeng.analytics.MobclickAgent;
import com.upload.BusinessModule;
import com.upload.ImgUpload;
import com.upload.UpLoadListener;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 投诉订单详情
 * Created by Administrator on 2019/6/11.
 */

public class ComplaintDetailsActivity extends ClientBasicActivity {
    /**
     * 单选
     */
    public final static int MODE_SINGLE = 0;
    /**
     * 多选
     */
    public final static int MODE_MULTI = 1;

    private static final int PICK_PHOTO = 1;

    @Bind(R.id.tv_complaint_details_type)
    TextView mTvComplaintDetailsType;
    @Bind(R.id.tv_complaint_details_content)
    TextView mTvComplaintDetailsContent;
    @Bind(R.id.et_complaint_evaluation)
    EditText mEtComplaintEvaluation;

    @Bind(R.id.img_evaluation_photo01)
    ImageView mImgEvaluationPhoto01;
    @Bind(R.id.img_evaluation_delete01)
    ImageView mImgEvaluationDelete01;

    @Bind(R.id.img_evaluation_photo02)
    ImageView mImgEvaluationPhoto02;
    @Bind(R.id.img_evaluation_delete02)
    ImageView mImgEvaluationDelete02;

    @Bind(R.id.img_evaluation_photo03)
    ImageView mImgEvaluationPhoto03;
    @Bind(R.id.img_evaluation_delete03)
    ImageView mImgEvaluationDelete03;

    @Bind(R.id.tv_complaint_details_button)
    TextView mTvComplaintDetailsButton;

    private SysDicBean mSysDicBean;
    //路径集合
    //private ArrayList<String> mPhotoPathList = new ArrayList<String>();
    // 待上传图片的路径集合
    private ArrayList<String> mPathList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_details);
        ButterKnife.bind(this);
        openEventBus();
        mSysDicBean = (SysDicBean) getIntent().getExtras().getSerializable("sys_dicbean");
        initHead();
        initView();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("订单投诉");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    public void initView(){
        mTvComplaintDetailsType.setText(mSysDicBean.getCodeText());
        mTvComplaintDetailsContent.setText(mSysDicBean.getCodeText2());
    }

    @OnClick({R.id.img_evaluation_photo01,R.id.img_evaluation_photo02,R.id.img_evaluation_photo03,
    R.id.img_evaluation_delete01,R.id.img_evaluation_delete02,R.id.img_evaluation_delete03,
    R.id.tv_complaint_details_button})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.img_evaluation_photo01:
                PhotoPick();
                break;
            case R.id.img_evaluation_photo02:
                PhotoPick();
                break;
            case R.id.img_evaluation_photo03:
                PhotoPick();
                break;
            case R.id.img_evaluation_delete01:
                if (mPathList.size() > 0) {
                    mPathList.remove(0);
                    initPhotoLayout();
                }
                break;
            case R.id.img_evaluation_delete02:
                if (mPathList.size() > 1) {
                    mPathList.remove(1);
                    initPhotoLayout();
                }
                break;
            case R.id.img_evaluation_delete03:
                if (mPathList.size() > 2) {
                    mPathList.remove(2);
                    initPhotoLayout();
                }
                break;
            case R.id.tv_complaint_details_button:
                if (TextUtils.isEmpty(mEtComplaintEvaluation.getText().toString())){

                    Toaster.show(mBaseContext,"评价内容不能为空");
                    return;
                }
                initData();
                MobclickAgent.onEvent(this, "complaint_details_button");
                break;
        }
    }

    public void PhotoPick(){
        Intent intent=new Intent();
        intent.setClass(mBaseContext, PhotoPickerActivity.class);
        intent.putExtra(PhotoPickerActivity.EXTRA_SHOW_CAMERA, true);//是否显示相机
        intent.putExtra(PhotoPickerActivity.EXTRA_SELECT_MODE, MODE_SINGLE);//单选还是多选
        intent.putExtra(PhotoPickerActivity.EXTRA_MAX_MUN, 1);//最多可选择数量
        startActivityForResult(intent, PICK_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PHOTO) {
            if (resultCode == RESULT_OK) {
                ArrayList<String> result = data.getStringArrayListExtra(PhotoPickerActivity.KEY_RESULT);
                //mPhotoPathList.add(result.get(0));
                uploadImg(result.get(0));
            }
        }
    }

    //图片上传
    public void uploadImg(String img){
        new ImgUpload(mBaseContext, UrlPool.STORAGE_UPLOAD, img, new UpLoadListener() {
            @Override
            public void onComplete(boolean isSuccess, String response) {

                if (isSuccess) {
                    UploadBean muploadbean = JSON.parseObject(response, UploadBean.class);
                    mPathList.add(muploadbean.getUrl());
                    initPhotoLayout();
                } else {
                    Toaster.show(getApplicationContext(), "上传失败，请重试");
                }
            }
        }, TAG).setBusinessModule(BusinessModule.Head).doUpload();
    }

    public void initData(){
        Params params=new Params();
        String imgs="";
        if (mPathList.size()>0){
            for (int i=0;i<mPathList.size();i++){
                imgs=imgs+mPathList.get(i).toString();
            }
        }
        params.add("imgs",imgs);
        params.add("orderCode",mSysDicBean.getOrderCode());
        params.add("reasonDetail",mEtComplaintEvaluation.getText().toString());
        params.add("reasonTitle",mSysDicBean.getCodeText());
        AuzHttp.post(UrlPool.POST_ORDER_COMPLAINT, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code==0){

                Toast toast = Toast.makeText(mBaseContext, "投诉申请成功", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                Intent intent=new Intent();
                intent.setClass(mBaseContext, OrderDetailsActivity.class);
                intent.putExtra("order_code",mSysDicBean.getOrderCode());
                startActivity(intent);
                finish();
                }else{
                    PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin) {
                       initData();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        },TAG);

    }

    private void initPhotoLayout() {
        int photoCount = mPathList.size();
        Bitmap addImage = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_upload);
        if (photoCount == 0) {
            mImgEvaluationDelete01.setVisibility(View.INVISIBLE);
            mImgEvaluationDelete02.setVisibility(View.INVISIBLE);
            mImgEvaluationDelete03.setVisibility(View.INVISIBLE);

            mImgEvaluationPhoto01.setImageBitmap(addImage);
            mImgEvaluationPhoto02.setVisibility(View.INVISIBLE);
            mImgEvaluationPhoto03.setVisibility(View.INVISIBLE);
        } else if (photoCount == 1) {
            mImgEvaluationDelete01.setVisibility(View.VISIBLE);
            mImgEvaluationDelete02.setVisibility(View.INVISIBLE);
            mImgEvaluationDelete03.setVisibility(View.INVISIBLE);

            mImgEvaluationPhoto01.setImageBitmap(addImage);
            mImgEvaluationPhoto02.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto03.setVisibility(View.INVISIBLE);

            mImgEvaluationDelete01.setVisibility(View.VISIBLE);
            Glide.with(this).load(mPathList.get(0)).into(mImgEvaluationPhoto01);

        } else if (photoCount == 2) {
            mImgEvaluationDelete01.setVisibility(View.VISIBLE);
            mImgEvaluationDelete02.setVisibility(View.VISIBLE);
            mImgEvaluationDelete03.setVisibility(View.INVISIBLE);

            mImgEvaluationPhoto01.setImageBitmap(addImage);
            mImgEvaluationPhoto01.setVisibility(View.VISIBLE);

            mImgEvaluationDelete01.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto02.setVisibility(View.VISIBLE);

            mImgEvaluationDelete02.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto03.setVisibility(View.VISIBLE);

            Glide.with(this).load(mPathList.get(0)).into(mImgEvaluationPhoto01);
            Glide.with(this).load(mPathList.get(1)).into(mImgEvaluationPhoto02);
        } else if (photoCount == 3) {

            mImgEvaluationDelete01.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto01.setVisibility(View.VISIBLE);

            mImgEvaluationDelete02.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto02.setVisibility(View.VISIBLE);

            mImgEvaluationDelete03.setVisibility(View.VISIBLE);
            mImgEvaluationPhoto03.setVisibility(View.VISIBLE);

            Glide.with(mBaseContext).load(mPathList.get(0)).into(mImgEvaluationPhoto01);
            Glide.with(mBaseContext).load(mPathList.get(1)).into(mImgEvaluationPhoto02);
            Glide.with(mBaseContext).load(mPathList.get(2)).into(mImgEvaluationPhoto03);
        }

    }

}
